﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models;

namespace AceMobile.Controllers
{ 
    [Authorize(Roles="Administrator")]
    public class AccountManageController : Controller
    {
        private MobileWebContext db = new MobileWebContext();

        //
        // GET: /AccountManage/

        public ViewResult Index()
        {
            var he = db.Accounts.ToList();
            IEnumerable<Account> haha = he.OrderByDescending(a => a.ID);
            
            return View(db.Accounts.Include("Role").ToList());
        }

        //
        // GET: /AccountManage/Details/5

        public ViewResult Details(int id)
        {
            Account account = db.Accounts.Include("Role").Where(a => a.ID == id).First();
            return View(account);
        }

        //
        // GET: /AccountManage/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AccountManage/Create

        [HttpPost]
        public ActionResult Create(Account account)
        {
            if (ModelState.IsValid)
            {
                var u = db.Accounts.Where(a => a.UserName == account.UserName);
                if (u.Count() > 0)
                {
                    ViewBag.mess = "Tài khoản đã tồn tại";
                    return View(account);
                }
                account.Role = db.Roles.Where(a => a.Name == "customer").First();
                db.Accounts.Add(account);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            ViewBag.mess = "Lỗi form gửi lên";
            return View(account);
        }
        
        //
        // GET: /AccountManage/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (db.Accounts.Find(id).Role.Name == "Administrator")
            {
                return RedirectToAction("Index");
            }
            Account account = db.Accounts.Include("Role").Where(a => a.ID == id).First();
            var roles = db.Roles.ToList();
            roles.Remove(roles.Where(a => a.Name == "Administrator").First());
            SelectList li = new SelectList(roles, "ID", "Name", account.Role.ID);
            ViewBag.Role = li;
            return View(account);
        }

        //
        // POST: /AccountManage/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection fm)
        {
            var linq = db.Accounts.Find(Convert.ToInt32(fm["ID"]));                       
            if (linq != null)
            {
                if (linq.UserName == fm["UserName"])
                {
                    linq.Password = fm["PassWord"];
                    linq.Role = db.Roles.Find(Convert.ToInt32(fm["Role"]));
                    if (linq.Role != null)
                    {
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                }                
            }
            return RedirectToAction("Edit", new { id = Convert.ToInt32(fm["ID"]) });
        }       
         
        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                Account account = db.Accounts.Find(id);
                if (account.Role.Name == "Administrator")
                {
                    return Json(new { status = "Administrator" });    
                }
                db.Accounts.Remove(account);
                db.SaveChanges();
                return Json(new { status = "Success" });
            }
            catch (Exception)
            {
                return Json(new { status = "NotSuccess" });
            }
            
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}